package com.sky.controller.admin;


import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
@RequiredArgsConstructor
@Api(tags = "套餐管理相关接口")
public class SetmealController {

    private final SetmealService setmealService;

    //新增套餐
    @PostMapping
    @ApiOperation("新增套餐信息")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增的菜品信息为：{}",setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }


    //分页查询套餐
    @GetMapping("/page")
    @ApiOperation("分页查询套餐信息")
    public Result<PageResult> query( SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult = setmealService.query(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    //修改套餐起售停售状态
    @PostMapping("/status/{status}")
    @ApiOperation("修改套餐起售停售状态")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result setStatus(@PathVariable Integer status,Long id){
        setmealService.setStatus(status,id);
        return Result.success();
    }

    //修改套餐
    //1、根据id查询套餐
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id){
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

    //2、修改套餐数据
    @PutMapping
    @ApiOperation("修改套餐数据")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setmealService.update(setmealDTO);
        return Result.success();
    }




    //批量删除套餐
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteByIds(@RequestParam List<Long> ids){
        setmealService.deleteByIds(ids);
        return Result.success();
    }


}
